#define  _CRT_SECURE_NO_WARNINGS

#include<iostream>
#include<assert.h>
using namespace std;

namespace byh
{
	class string
	{
	public:
		static const size_t npos;

		//constructor
		string(const char* str = "")
			:_size(strlen(str))
		{
			_capacity = _size;
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}

		//copy constructor
		// string s2(s1)
		string(const string& s)
		{
			//_str = new char[_capacity + 1];
			//strcpy(_str, s.c_str());
			//_size = s._size;
			//_capacity = s._capacity;

			string temp(s.c_str());
			swap(temp);
		}

		//assignment operator overloading
		// s1 = s2
		//string& operator=(const string& s)
		//{
		//	char* temp = new char[s._capacity + 1];
		//	strcpy(temp, s._str);
		//	delete[] _str;
		//	_str = temp;
		//	_size = s._size;
		//	_capacity = s._capacity;
		//	return *this;
		//}

		// s1 = s2
		string& operator=(string s)
		{
			swap(s);
			return *this;
		}

		//destructor
		~string()
		{
			delete[] _str;
			_str = nullptr;
			_size = _capacity = 0;
		}

		//iterator
		typedef char* iterator;
		typedef const char* const_iterator;

		iterator begin()
		{
			return _str;
		}

		iterator end()
		{
			return _str + _size;
		}

		const_iterator begin() const
		{
			return _str;
		}

		const_iterator end() const
		{
			return _str + _size;
		}

		//capacity
		size_t size() const
		{
			return _size;
		}

		size_t capacity() const
		{
			return _capacity;
		}

		void resize(size_t n, char ch = '\0')
		{
			if (n < _size)
			{
				_str[n] = '\0';
				_size = n;
			}
			else
			{
				reserve(n);
				for (int i = _size; i < n; i++)
				{
					_str[i] = ch;
				}
				_str[n] = '\0';
				_size = n;
			}
		}

		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* temp = new char[n + 1];
				strcpy(temp, _str);
				_str = temp;
				_capacity = n;
			}
		}

		//modify
		void push_back(char ch)
		{
			insert(_size, ch);
		}

		string& operator+=(char ch)
		{
			insert(_size, ch);
			return *this;
		}

		string& operator+=(const char* str)
		{
			insert(_size, str);
			return *this;
		}

		void append(const char* str)
		{
			insert(_size, str);
		}

		void clear()
		{
			_str[0] = '\0';
			_size = 0;
		}

		void insert(size_t pos, char ch)
		{
			assert(pos <= _size);

			if (_size == _capacity)
				reserve(_capacity == 0 ? 4 : 2*_capacity);
			
			size_t end = _size + 1;
			while (end > pos)
			{
				_str[end] = _str[end - 1];
				end--;
			}
			_str[pos] = ch;
			_size += 1;
		}

		void insert(size_t pos, const char* str)
		{
			assert(pos <= _size);

			size_t len = strlen(str);
			if (_size + len > _capacity)
				reserve(_size + len);

			size_t end = _size + len;
			while (end > pos + len - 1)
			{
				_str[end] = _str[end - len];
				end--;
			}
			strncpy(_str + pos, str, len);
			_size += len;
		}

		void erase(size_t pos, size_t len = npos)
		{
			assert(pos < _size);

			if (len == npos || len >= _size - pos)
			{
				_str[pos] = '\0';
				_size = pos;
			}
			else
			{
				strcpy(_str + pos, _str + pos + len);
				_size -= len;
			}
		}

		//swap(s1,s2)
		void swap(string& s)
		{
			std::swap(_str, s._str);
			std::swap(_size, s._size);
			std::swap(_capacity, s._capacity);
		}

		//access
		char& operator[](size_t pos)
		{
			return _str[pos];
		}

		const char& operator[](size_t pos) const
		{
			return _str[pos];
		}

		//string operations
		const char* c_str() const
		{
			return _str;
		}

		size_t find(char ch, size_t pos = 0) const
		{
			assert(pos < _size);

			for (size_t i = pos; i < _size; i++)
			{
				if (_str[i] == ch)
					return i;
			}
			return npos;
		}

		size_t find(const char* str, size_t pos = 0) const
		{
			assert(pos < _size);

			char* temp = strstr(_str + pos, str);
			if (temp)
				return (temp - _str);
			else
				return npos;
		}

		string substr(size_t pos = 0, size_t len = npos) const
		{
			assert(pos < _size);

			string temp;
			if (len == npos || _size - pos <= len)
			{
				for (size_t i = pos; i < _size; i++)
				{
					temp += _str[i];
				}
			}
			else
			{
				for (size_t i = pos; i < pos + len; i++)
				{
					temp += _str[i];
				}
			}
			return temp;
		}

	private:
		char* _str = nullptr;
		size_t _size = 0;
		size_t _capacity = 0;
	};

	const size_t string::npos = -1;

	//Non-member function overload
	bool operator==(const string& s1, const string& s2)
	{
		return strcmp(s1.c_str(), s2.c_str()) == 0;
	}

	bool operator!=(const string& s1, const string& s2)
	{
		return !(s1 == s2);
	}

	bool operator>(const string& s1, const string& s2)
	{
		return strcmp(s1.c_str(), s2.c_str()) > 0;
	}

	bool operator>=(const string& s1, const string& s2)
	{
		return (s1 > s2 || s1 == s2);
	}

	bool operator<(const string& s1, const string& s2)
	{
		return strcmp(s1.c_str(), s2.c_str()) < 0;
	}

	bool operator<=(const string& s1, const string& s2)
	{
		return !(s1 > s2);
	}

	ostream& operator<<(ostream& out, const string& s)
	{
		out << s.c_str();
		return out;
	}

	istream& operator>>(istream& in, string& s)
	{
		s.clear();
		char ch = 0;
		char temp[128] = { 0 };
		int i = 0;

		ch = in.get();
		while (ch != ' ' && ch != '\n')
		{
			temp[i++] = ch;

			if (127 == i)
			{
				temp[i] == '\0';
				s += temp;
				i = 0;
			}

			ch = in.get();
		}

		if (i > 0)
		{
			temp[i] == '\0';
			s += temp;
		}
		return in;
	}

	void swap(string& s1, string& s2)
	{
		s1.swap(s2);
	}

	void getline(istream& in, string& s)
	{
		s.clear();
		char ch = 0;
		char temp[128] = { 0 };
		int i = 0;

		ch = in.get();
		while (ch != '\n')
		{
			temp[i++] = ch;

			if (127 == i)
			{
				temp[i] == '\0';
				s += temp;
				i = 0;
			}

			ch = in.get();
		}

		if (i > 0)
		{
			temp[i] == '\0';
			s += temp;
		}
	}

	void Test_string()
	{
		//string s1;
		//string s2("Hello World");
		//cout << s1.c_str() << endl;
		//cout << s2.c_str() << endl;

		//string s1("Hello World");
		//string s2(s1);
		//cout << s1.c_str() << endl;
		//cout << s2.c_str() << endl;

		//string s1("Hello World");
		//string s2;
		//s2 = s1;
		//cout << s1.c_str() << endl;
		//cout << s2.c_str() << endl;

		//string s("Hello World");
		//string::iterator it = s.begin();
		//while (it != s.end())
		//{
		//	cout << *it << " ";
		//	it++;
		//}
		//cout << endl;

		//string s("Hello World");
		//cout << s.capacity() << endl;
		//s.reserve(10);
		//cout << s.capacity() << endl;

		//string s("Hello World");
		//s.resize(20, 'c');
		//cout << s.c_str() << endl;

		//string s("Hello World");
		//s.insert(0, 'x');
		//cout << s.c_str() << endl;
		//s.insert(6, 'x');
		//cout << s.c_str() << endl;
		//s.insert(0, "xxx");
		//cout << s.c_str() << endl;
		//s.insert(6, "yyyllloooddd");
		//cout << s.c_str() << endl;
		//s.insert(0, "");
		//cout << s.c_str() << endl;

		//string s("Hello World");
		//s.erase(0, 3);
		//cout << s.c_str() << endl;
		//s.erase(0);
		//cout << s.c_str() << endl;

		//string s("Hello World");
		//s.push_back('x');
		//cout << s.c_str() << endl;
		//s += 'o';
		//cout << s.c_str() << endl;
		//s += "yyy";
		//cout << s.c_str() << endl;
		//s.append("mmmm");
		//cout << s.c_str() << endl;

		//string s1("Hello World");
		//string s2("Hello bit");
		//s1.swap(s2);
		//cout << s1.c_str() << endl;
		//cout << s2.c_str() << endl;

		//string s1("Hello World");
		//string s2(s1);
		//cout << s2.c_str() << endl;
		//string s3;
		//s3 = s1;
		//cout << s3.c_str() << endl;

		//string s1("Hello World");
		//cout << s1.find('o') << endl;
		//cout << s1.find("llo") << endl;
		//cout << s1.find("lllo") << endl;

		string s("https://legacy.cplusplus.com/reference/cstring");
		string protocol;
		string domain;
		string uri;

		size_t pos1 = s.find(':');
		if (pos1 != string::npos)
			protocol = s.substr(0, pos1-0);

		size_t pos2 = s.find('/', pos1 + 3);
		if (pos2 != string::npos)
		{
			domain = s.substr(pos1 + 3, pos2 - pos1 - 3);
			uri = s.substr(pos2 + 1);
		}

		cout << protocol.c_str() << endl;
		cout << domain.c_str() << endl;
		cout << uri.c_str() << endl;
		
		//string s("Hello World");
		//for (size_t i = 0; i < s.size(); i++)
		//{
		//	cout << s[i] << " ";
		//}
		//cout << endl;
		//s.clear();
		//cout << s.c_str() << endl;

		//string s1("Tassel");
		//string s2("Faker");
		//cout << (s1 == s2) << endl;
		//cout << (s1 != s2) << endl;
		//cout << (s1 > s2) << endl;
		//cout << (s1 >= s2) << endl;
		//cout << (s1 < s2) << endl;
		//cout << (s1 <= s2) << endl;

		//string s1("Hello World");
		//string s2("Hello bit");
		//cout << s1 << " " << s2 << endl;

		//string s1("Hello World");
		//string s2("Hello Bit");
		//cin >> s1 >> s2;
		//cout << s1 << endl;
		//cout << s2 << endl;

		//string s1;
		//getline(cin, s1);
		//cout << s1 << endl;
	}
}